//
//  SMMineRecentCell.m
//  SteelRecover
//
//  Created by 郑渊文 on 2025/9/17.
//

#import "SMMineRecentCell.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/UIImageView+WebCache.h>

@interface SMMineRecentCell ()
@property(nonatomic, strong) UIView *card;
@property(nonatomic, strong) UIImageView *thumb;
@property(nonatomic, strong) UILabel *titleL;
@end

@implementation SMMineRecentCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self=[super initWithFrame:frame]) {
        self.contentView.backgroundColor = UIColor.clearColor;

        _card = [UIView new];
        _card.backgroundColor = UIColor.secondarySystemBackgroundColor;
        _card.layer.cornerRadius = 12;
        _card.layer.masksToBounds = YES;
        [self.contentView addSubview:_card];

        _thumb = [UIImageView new];
        _thumb.contentMode = UIViewContentModeScaleAspectFill;
        _thumb.clipsToBounds = YES;

        _titleL = [UILabel new];
        _titleL.font = [UIFont systemFontOfSize:13 weight:UIFontWeightSemibold];
        _titleL.textColor = UIColor.labelColor;
        _titleL.numberOfLines = 2;

        [_card addSubview:_thumb];
        [_card addSubview:_titleL];

        [_card mas_makeConstraints:^(MASConstraintMaker *make){
            make.edges.equalTo(self.contentView);
        }];
        [_thumb mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.left.right.equalTo(self.card);
            make.height.equalTo(self.card).multipliedBy(0.68);
        }];
        [_titleL mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.equalTo(self.thumb.mas_bottom).offset(6);
            make.left.equalTo(self.card).offset(8);
            make.right.equalTo(self.card).offset(-8);
            make.bottom.lessThanOrEqualTo(self.card).offset(-8);
        }];
    }
    return self;
}

- (void)prepareForReuse {
    [super prepareForReuse];
    [_thumb sd_cancelCurrentImageLoad];
    _thumb.image = nil;
}

- (void)configureWithTitle:(NSString *)title imageURL:(NSString *)url {
    _titleL.text = title ?: @"";
    UIImage *ph = nil;
    if (@available(iOS 13.0, *)) ph = [UIImage systemImageNamed:@"photo"];
    [_thumb sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:ph];
}

@end
